<?php

use hhz\constant\redis\redisKeys;
use Hhz\Redis\otherRedis;

/**
 * Description: 订阅盲盒活动
 * Created by PhpStorm.
 * User: congyukun
 * Date: 2021/4/14
 * Time: 14:49
 */

class SubBoxAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;


    public function main()
    {
        try {
            $action_from = $this->getRequest()->getPost('action_from', 'blind_box_sub');
            $uid = $this->uid;
            if (!$uid) {
                throw new \UnexpectedValueException('参数错误');
            }
            $key = $uid . '_add_sub_' . date("Y-m-d");
            //5秒禁止提交
            $lock = \Biz\cache\brand\BrandShowActivity::isLock($key, 5);
            if ($lock) {
                throw new \UnexpectedValueException('请不要频繁提交');
            }
            \Biz\cache\brand\BlindBox::addSubPush((array)$uid);
            //迁移使用新的redis方案
//            $constKey = redisKeys::KEY_B_OTHER_BLIND_BOX_SUB_;
//            $redis = new otherRedis($constKey);
//            $sKey = $constKey. date("Y-m");
//            $redis->sadd($sKey,(array)$uid);


            $msg = '订阅成功';
        } catch (UnexpectedValueException $e) {
            return $this->setSuccess(['msg' => $e->getMessage()], 0);
        } catch (Exception $e) {
            \Doraemon\tools\Log::error('品牌盲盒关注失败-error:' . $e->getMessage(), $this->getRequest()->getPost());
            return $this->setSuccess(['msg' => '系统错误: ' . $e->getMessage()], 0);
        }
        \Doraemon\tools\ActionLog::log($this->uid, ['uid' => $this->uid, 'action_from' => $action_from], \Doraemon\tools\ActionLog::BLIND_BOX_SUB);
        $this->setSuccess(['msg' => $msg]);
    }
}